package com.gidgabs.modules.api.controller;

import com.gidgabs.common.utils.DeleteType;
import com.gidgabs.common.utils.ReturnJson;
import com.gidgabs.common.validator.Assert;
import com.gidgabs.dto.TbButlerOrderDto;
import com.gidgabs.modules.api.service.TbButlerOrderService;
import com.gidgabs.modules.entity.TbButlerOrderEntity;
import com.gidgabs.utils.OrderUtil;
import com.gidgabs.utils.SessionDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 跑腿订单表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-07-02 10:07:40
 */
@Api("跑腿订单模块")
@RestController
@RequestMapping("/api/butlerorder")
public class TbButlerOrderController {

    @Autowired
    private TbButlerOrderService butlerOrderService;

    /**
     * 查询我的订单列表 根据订单状态 创建时间排序
     *
     * @param pageSize
     * @param pageNo
     * @return
     */
    @ApiOperation(value = "查询我的订单列表 根据订单状态 创建时间排序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态 0 等待支付 1 等待接单 2 进行中 3 完成", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "selectType", value = "查询类型 1 自己的 2 所有的 订单", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @GetMapping("getOrderList")
    public ReturnJson<TbButlerOrderDto> getOrderList(Integer pageSize, Integer pageNo, Integer orderStatus, Integer selectType) {
        Assert.isNull(orderStatus, "订单状态不能为空");
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        //如果是查询 所有的订单 那么只能查询 等待接单状态的订单
        if (selectType == 2) {
            if (orderStatus != 1) {
                return ReturnJson.success("查询所有订单 只能查询待接单状态的");
            }
        }
        return butlerOrderService.selectOrderList(pageSize, pageNo, orderStatus, selectType);
    }

    /**
     * 根据跑腿订单id查询订单详细信息
     *
     * @param orderId
     * @return
     */
    @ApiOperation(value = "根据跑腿订单id查询订单详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @GetMapping("/info/{orderId}")
    public ReturnJson<TbButlerOrderDto> info(@PathVariable("orderId") Integer orderId) {
        TbButlerOrderEntity tbButlerOrderEntity = butlerOrderService.queryObject(orderId);
        return ReturnJson.success("查询成功", tbButlerOrderEntity.toDto());
    }

    /**
     * 创建跑腿订单
     *
     * @param tbButlerOrderDto
     * @return
     */
    @PostMapping("/addButlerOrder")
    @ApiOperation(value = "创建跑腿订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    public ReturnJson<String> addButlerOrder(@ModelAttribute TbButlerOrderDto tbButlerOrderDto) {
        tbButlerOrderDto.setSourceUserId(SessionDataUtil.getCurrentUserId());
        tbButlerOrderDto.setOrderCode(OrderUtil.StringtOrderNo());
        tbButlerOrderDto.setCreateTime(new Date());
        tbButlerOrderDto.setUpdateTime(new Date());
        tbButlerOrderDto.setDeleteType(DeleteType.NOT_DELETE.getKey());
        //默认待支付状态
        tbButlerOrderDto.setOrderStatus(0);
        butlerOrderService.save(tbButlerOrderDto.toEntity());
        return ReturnJson.success("添加成功");
    }

    /**
     * 接订单
     *
     * @param orderId
     * @return
     */
    @PostMapping("/getButlerOrder")
    @ApiOperation(value = "接订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单编号", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    public ReturnJson<String> getButlerOrder(Integer orderId) {
        TbButlerOrderEntity tbButlerOrderEntity = butlerOrderService.queryObject(orderId);
        int userId = SessionDataUtil.getCurrentUserId();
        //判断当前订单是否是 登录人发布的 不能自己接自己的单
        if (tbButlerOrderEntity.getSourceUserId() == userId) {
            return ReturnJson.fail("接单失败 不能接自己的订单哦");
        }
        //封装
        tbButlerOrderEntity.setUserId(userId);
        tbButlerOrderEntity.setPickUpNow(new Date());
        tbButlerOrderEntity.setOrderStatus(2);
        butlerOrderService.update(tbButlerOrderEntity);
        return ReturnJson.success("接单成功");
    }

    /**
     * 跑腿订单确认完成
     *
     * @return
     */
    @PostMapping("/butlerOrderFinish")
    @ApiOperation(value = "跑腿订单确认完成")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单编号", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    public ReturnJson<String> butlerOrderFinish(Integer orderId) {
        Assert.isNull(orderId, "订单id不能为空");
        TbButlerOrderEntity tbButlerOrderEntity = butlerOrderService.queryObject(orderId);
        int userId = SessionDataUtil.getCurrentUserId();
        //判断当前订单是否是接单人
        if (tbButlerOrderEntity.getUserId() != userId) {
            return ReturnJson.fail("订单确认失败");
        }
        //修改状态为已完成
        tbButlerOrderEntity.setOrderStatus(3);
        tbButlerOrderEntity.setUpdateTime(new Date());
        butlerOrderService.update(tbButlerOrderEntity);
        return ReturnJson.success("订单完成");
    }

    /**
     * 支付订单
     *
     * @return
     */
    @PostMapping("/payOrder")
    @ApiOperation(value = "支付订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderCode", value = "订单编号", dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    public ReturnJson<String> payOrder(String orderCode, Integer money) {
        Assert.isNull(orderCode, "订单编号不能为空");
        Assert.isNull(money, "价格不能为空");
        return butlerOrderService.payOrder(orderCode, money);
    }

}
